package com.george.service.impl;

import com.george.model.entity.User;
import com.george.dao.UserMapper;
import com.george.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author George Chan
 * @since 2019-09-28
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private BCryptPasswordEncoder encoder;

    @Override
    public User findById(int id) {
        User user = this.baseMapper.selectById(id);
        return user;
    }

    @Override
    public User findLoginUser(String username, String password) {
        // 用户是否存在
        User user = this.baseMapper.findLoginUser(username);
        // 校验密码
        if (!Objects.isNull(user) && encoder.matches(password, user.getPassword())){
            return user;
        }
        return null;
    }
}
